﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.AspNetCore.Http
{
    public static class HttpResponseJsonExtensions
    {
        /// <summary>
        /// 输出Newtonsoft.Json序列化结果
        /// </summary>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="response"></param>
        /// <param name="value"></param>
        /// <param name="options"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public static Task WriteAsJsonAsync<TValue>(this HttpResponse response, TValue value, JsonSerializerSettings? options, CancellationToken cancellationToken = default)
        {
            var payload = JsonConvert.SerializeObject(value, options);
            response.ContentType = "application/json";
            return response.WriteAsync(payload, cancellationToken);
        }
    }
}
